home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / User / pwent.pm < prev   
Encoding:
Perl POD Document  |  1999-12-28  |  2.9 KB  |  103 lines

  1. package User::pwent;
  2. use strict;
  3.  
  4. BEGIN { 
  5.     use Exporter   ();
  6.     use vars       qw(@EXPORT @EXPORT_OK %EXPORT_TAGS);
  7.     @EXPORT      = qw(getpwent getpwuid getpwnam getpw);
  8.     @EXPORT_OK   = qw(
  9.             $pw_name   $pw_passwd     $pw_uid     
  10.             $pw_gid       $pw_quota    $pw_comment
  11.             $pw_gecos  $pw_dir    $pw_shell
  12.            );
  13.     %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
  14. }
  15. use vars      @EXPORT_OK;
  16.  
  17. sub import { goto &Exporter::import }
  18.  
  19. use Class::Struct qw(struct);
  20. struct 'User::pwent' => [
  21.     name    => '$',
  22.     passwd  => '$',
  23.     uid        => '$',
  24.     gid        => '$',
  25.     quota   => '$',
  26.     comment => '$',
  27.     gecos   => '$',
  28.     dir        => '$',
  29.     shell   => '$',
  30. ];
  31.  
  32. sub populate (@) {
  33.     return unless @_;
  34.     my $pwob = new();
  35.  
  36.     ( $pw_name,   $pw_passwd,   $pw_uid,  
  37.       $pw_gid,    $pw_quota,    $pw_comment,
  38.       $pw_gecos,  $pw_dir,      $pw_shell,   )     = @$pwob = @_;
  39.  
  40.     return $pwob;
  41.  
  42. sub getpwent ( ) { populate(CORE::getpwent()) } 
  43. sub getpwnam ($) { populate(CORE::getpwnam(shift)) } 
  44. sub getpwuid ($) { populate(CORE::getpwuid(shift)) } 
  45. sub getpw    ($) { ($_[0] =~ /^\d+/) ? &getpwuid : &getpwnam } 
  46.  
  47. 1;
  48. __END__
  49.  
  50. =head1 NAME
  51.  
  52. User::pwent - by-name interface to Perl's built-in getpw*() functions
  53.  
  54. =head1 SYNOPSIS
  55.  
  56.  use User::pwent;
  57.  $pw = getpwnam('daemon') or die "No daemon user";
  58.  if ( $pw->uid == 1 && $pw->dir =~ m#^/(bin|tmp)?$# ) {
  59.      print "gid 1 on root dir";
  60.  } 
  61.  
  62.  use User::pwent qw(:FIELDS);
  63.  getpwnam('daemon') or die "No daemon user";
  64.  if ( $pw_uid == 1 && $pw_dir =~ m#^/(bin|tmp)?$# ) {
  65.      print "gid 1 on root dir";
  66.  } 
  67.  
  68.  $pw = getpw($whoever);
  69.  
  70. =head1 DESCRIPTION
  71.  
  72. This module's default exports override the core getpwent(), getpwuid(),
  73. and getpwnam() functions, replacing them with versions that return
  74. "User::pwent" objects.  This object has methods that return the similarly
  75. named structure field name from the C's passwd structure from F<pwd.h>; 
  76. namely name, passwd, uid, gid, quota, comment, gecos, dir, and shell.
  77.  
  78. You may also import all the structure fields directly into your namespace
  79. as regular variables using the :FIELDS import tag.  (Note that this still
  80. overrides your core functions.)  Access these fields as
  81. variables named with a preceding C<pw_> in front their method names.
  82. Thus, C<$passwd_obj-E<gt>shell()> corresponds to $pw_shell if you import
  83. the fields.
  84.  
  85. The getpw() funtion is a simple front-end that forwards
  86. a numeric argument to getpwuid() and the rest to getpwnam().
  87.  
  88. To access this functionality without the core overrides,
  89. pass the C<use> an empty import list, and then access
  90. function functions with their full qualified names.
  91. On the other hand, the built-ins are still available
  92. via the C<CORE::> pseudo-package.
  93.  
  94. =head1 NOTE
  95.  
  96. While this class is currently implemented using the Class::Struct
  97. module to build a struct-like class, you shouldn't rely upon this.
  98.  
  99. =head1 AUTHOR
  100.  
  101. Tom Christiansen
  102.